<%-- 
    Document   : DoSearch
    Created on : Dec 7, 2011, 1:35:11 PM
    Author     : hendrikde
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Collections" %>
<% if(session.getAttribute("LoggedIn")==null)
       {
    response.sendRedirect("index.html");
    return;
        }

%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Searching</title>
        
    </head>
    <body>
        <%
        String[] keywords = request.getParameter("keyword").split(" ");
        
        DBClasses.GetConnection gc = new DBClasses.GetConnection();
        java.sql.Connection conn = gc.getConnection();
        
        try {
            java.sql.Statement stmt = conn.createStatement();
            String query = "SELECT C.CircleName, C.CircleType, CONCAT(U.FirstName, ' ', U.LastName), C.CircleID "
                    + "FROM Circle C, Customer U "
                    + "WHERE U.CustomerID = C.CircleOwner";
            
            java.sql.ResultSet rs = stmt.executeQuery(query);
            ArrayList<DBClasses.SearchNode> circset = new ArrayList<DBClasses.SearchNode>();
            int i = 0;
            while (rs.next()) {
                String[] inputs = {rs.getString(1), rs.getString(2), rs.getString(3)};
                DBClasses.SearchNode sn = new DBClasses.SearchNode(inputs, Integer.parseInt(rs.getString(4)));
                String w = sn.getWords();
                for (int r = 0; r < keywords.length; r++) {
                    if (w.contains((CharSequence) keywords[r]))
                        sn.increment();
                }
                if (sn.getCount() > 0)
                    circset.add(sn);
                i++;
            }
            Collections.sort(circset);
            session.setAttribute("CircleSet", circset);
            
            
           
        }
        catch(Exception e) {
            e.printStackTrace();
            response.sendRedirect("SearchCircles.jsp");
        }
        
        finally
    {
        try{
            conn.commit();
            conn.close();
            response.sendRedirect("SearchCircles.jsp");
        }
        catch(Exception ee)
                               {
                ee.printStackTrace();
            }
        }
         
        %>
    </body>
</html>
